package com.sftz.modules.minapp.home.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Cnd;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import com.sftz.assets.entities.CourseEntity;
import com.sftz.assets.entities.CourseTypeEntity;
import com.sftz.assets.entities.FollowingEntity;
import com.sftz.assets.entities.SeriesCoursesEntity;
import com.sftz.assets.entities.WxCarouselEntity;
import com.sftz.assets.enums.AttentionEnum;
import com.sftz.assets.enums.CourseStateEnum;
import com.sftz.assets.enums.InsCourseTypeEnum;
import com.sftz.assets.enums.SerCourseStatusEnum;
import com.sftz.assets.enums.YesOrNoEnum;
import com.sftz.modules.minapp.home.dto.BigVDto;
import com.sftz.modules.minapp.home.form.HomeForm;
import com.sftz.modules.minapp.user.cache.RedissionUserCache;
import com.sftz.modules.minapp.user.cache.WxSessionKeyCache;
import com.uxuexi.core.common.util.Util;
import com.uxuexi.core.db.util.DbSqlUtil;
import com.uxuexi.core.web.base.service.BaseService;

@IocBean
public class HotrecommendViewService extends BaseService<CourseEntity> {
	private static final Log log = Logs.get();

	private static final int followed = 1;//已关注

	private static final int unfollowed = 2;//未关注

	/*
	 *（用途:首页轮播图）
	 */
	public List<WxCarouselEntity> homeCarouseData() {

		//轮播图
		List<WxCarouselEntity> scarouse = dbDao.query(WxCarouselEntity.class,
				Cnd.where("wxIsShow", "=", YesOrNoEnum.YES.intKey()).asc("wxOrd"), null);

		return scarouse;

	}

	/*
	 *（用途:首页课程一级分类）
	 */
	public List<CourseTypeEntity> homeCourseTypeList() {

		//课程一级分类
		Sql courseSql = Sqls.create(sqlManager.get("minapp_home_courseType"));
		List<CourseTypeEntity> courseType = DbSqlUtil.query(dbDao, CourseTypeEntity.class, courseSql);

		return courseType;

	}

	/*
	 *（用途:首页学历非学历课程查询）
	 */
	public Object homeCourseList() {

		Map<String, Object> obj = new HashMap<String, Object>();
		//查询学历课程
		Sql eduCourseSql = Sqls.create(sqlManager.get("minapp_home_educationOrNo_course_list"));
		eduCourseSql.setParam("courseState", CourseStateEnum.PUBLISHED.intKey());
		eduCourseSql.setParam("insCourseType", InsCourseTypeEnum.EDUCATIONINCOURSES.intKey());
		List<CourseEntity> eduCourse = DbSqlUtil.query(dbDao, CourseEntity.class, eduCourseSql);
		//查询非学历课程
		Sql nonEduCourseSql = Sqls.create(sqlManager.get("minapp_home_educationOrNo_course_list"));
		nonEduCourseSql.setParam("courseState", CourseStateEnum.PUBLISHED.intKey());
		nonEduCourseSql.setParam("insCourseType", InsCourseTypeEnum.NOEDUCATIONINCOURSES.intKey());
		List<CourseEntity> nonEduCourse = DbSqlUtil.query(dbDao, CourseEntity.class, nonEduCourseSql);

		obj.put("eduCourseList", eduCourse);
		obj.put("nonEduCourseList", nonEduCourse);

		return obj;

	}

	/*
	 *（用途:首页推荐专业）
	 */
	public List<SeriesCoursesEntity> homeMajorListData() {

		//查询推荐专业
		List<SeriesCoursesEntity> SeriesCouList = dbDao.query(SeriesCoursesEntity.class,
				Cnd.where("id", "=", 3).or("id", "=", 4).desc("studyNum"), null);

		List<SeriesCoursesEntity> SeriesCou = dbDao.query(
				SeriesCoursesEntity.class,
				Cnd.where("id", "!=", 3).and("id", "!=", 4).and("status", "=", SerCourseStatusEnum.ON.intKey())
						.and("isDelete", "=", YesOrNoEnum.NO.intKey()).desc("studyNum"), null);
		if (!Util.isEmpty(SeriesCou)) {
			SeriesCouList.add(SeriesCou.get(0));
		}
		return SeriesCouList;

	}

	/*
	 *（用途:首页热门老师）
	 */
	public List<BigVDto> homeBigVData(final HomeForm form) {
		String token = form.getToken();

		//查询人气老师
		Sql hotBigVSql = Sqls.create(sqlManager.get("minapp_home_hotBigV"));
		hotBigVSql.setParam("fansStatus", AttentionEnum.ATTENTION.intKey());
		List<BigVDto> bigV = DbSqlUtil.query(dbDao, BigVDto.class, hotBigVSql);
		List<BigVDto> queryBigVuser = queryBigVuser(bigV, token);

		return queryBigVuser;

	}

	/*
	 *（用途:查询当卡登陆用户对热门老师的关注状态）
	 */
	public List<BigVDto> queryBigVuser(List<BigVDto> bigV, String token) {
		for (BigVDto queryById : bigV) {
			Integer id = queryById.getId();
			//如果没有登录,则状态均为未关注
			if (Util.isEmpty(token)) {
				queryById.setStatus(unfollowed);
			} else if (Util.isEmpty(WxSessionKeyCache.getSessionKey(token))) {
				queryById.setStatus(unfollowed);
			} else {
				Integer userId = RedissionUserCache.getUserId(token);
				//查询当前登陆者是否关注了此人
				FollowingEntity follow = dbDao.fetch(
						FollowingEntity.class,
						Cnd.where("userId", "=", userId).and("followingId", "=", id)
								.and("status", "=", AttentionEnum.ATTENTION.intKey()));
				if (Util.isEmpty(follow)) {
					queryById.setStatus(unfollowed);
				} else {
					queryById.setStatus(followed);
				}
			}
		}
		return bigV;
	}

}